import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zchope_student/apis.dart';
import 'package:zchope_student/beans/newUrlBean.dart';
import 'package:zchope_student/mine/mine.dart';
import 'package:zchope_student/utils/mydio.dart';

import '../const.dart';

class XueShi extends StatefulWidget {
  @override
  _XueShiState createState() => _XueShiState();
}

class _XueShiState extends State<XueShi> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  var lng;
  var lat;
  var _city = '';
  var _cityCode = '';
  var countiesMap;
  var _district = '请选择城市';
  var _districtCode = '';
  var pickerCity;
  var pickerCountry;
  var cities = [
    '石家庄市',
    '唐山市',
    '秦皇岛市',
    '邯郸市',
    '邢台市',
    '保定市',
    '张家口市',
    '承德市',
    '沧州市',
    '廊坊市',
    '衡水市'
  ];
  newUrlBean bean;
  var url = '';
  var recordBh = '';
  var personNum = '';

  _buildPickerCountry() {
    Picker(
      adapter: PickerDataAdapter(data: _setPickerCountries()),
      title: Text('请选择'),
      cancelText: '取消',
      confirmText: '确定',
      selectedTextStyle: TextStyle(color: Colors.blue),
      onConfirm: (Picker picker, List value) {
        setState(() {
          _district = cities[value[0]];
//          _districtCode = countries[value[0]].code;
          _getRegionByCity(_district);
        });
      },
    ).show(_scaffoldKey.currentState);
  }

  _setPickerCountries() {
    List<PickerItem<dynamic>> pickerItems = List();
    for (int i = 0; i < cities.length; i++) {
      pickerItems.add(PickerItem(
          value: Icons.add,
          text: Text(cities[i],
              style: TextStyle(color: const Color(0xff333333), fontSize: 16))));
    }
    return pickerItems;
  }

  void _getRegionByCity(String district) async {
    MyDio.getInstance().get(Apis.getRegionByCity + district, (data) async {
      print(data);
      setState(() {
        if (data['resultCode'] == 0) {
          //成功
          bean = newUrlBean.fromJson(data);
          url = bean.record.trainingurl +
              'api/queryTimePerson/130527199711200867';
          print(url);
        }
      });
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getPersonNum();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    MyDio.getInstance().clearInstance();
    super.dispose();
  }

  void _getPersonNum() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    personNum = sp.getString(Const.PERSON_NUM);
    recordBh = sp.getString(Const.UUID);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
            key: _scaffoldKey,
            appBar: AppBar(),
            body: Column(
              children: <Widget>[
                Container(
                  color: Colors.white,
                  height: 45,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                        horizontal: 15, vertical: 12),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        SizedBox(width: 14),
                        GestureDetector(
                          onTap: () {
                            pickerCity.show(_scaffoldKey.currentState);
                          },
                          child: SizedBox(
                            width: 75,
                            height: 20,
                            child: Text(
                              '河北省',
                              style: TextStyle(
                                  fontSize: 14, color: const Color(0xff333333)),
                            ),
                          ),
                        ),
                        SizedBox(width: 2),
                        Icon(
                          Icons.keyboard_arrow_down,
                          size: 18,
                        ),
                        SizedBox(width: 20),
                        GestureDetector(
                          onTap: () {
                            _buildPickerCountry();
                          },
                          child: SizedBox(
                            width: 75,
                            height: 20,
                            child: Text(
                              _district,
                              style: TextStyle(
                                  fontSize: 14, color: const Color(0xff333333)),
                            ),
                          ),
                        ),
                        SizedBox(width: 2),
                        Icon(
                          Icons.keyboard_arrow_down,
                          size: 18,
                        )
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            ));
  }
}
